package com.ruoyi.notice;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.utils.DictStrUtils;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/h5/notice/notice")
public class noticeControllerApp extends BaseController {

    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        List<SysNotice> sysNotices = sysNoticeMapper.selectNoticeList(null);
        for(SysNotice notice: sysNotices){
            //字典值翻译
            String dictLabel = DictUtils.getDictLabel(DictStrUtils.SYS_NOTICE_TYPE, notice.getNoticeType());
            notice.setNoticeType(dictLabel);
        }
        return getDataTable(sysNotices);
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysNotice sysNotice = sysNoticeMapper.selectNoticeById(id);
        //字典值翻译
        String dictLabel = DictUtils.getDictLabel(DictStrUtils.SYS_NOTICE_TYPE, sysNotice.getNoticeType());
        sysNotice.setNoticeType(dictLabel);
        //图片路径更换
        String url = sysNotice.getNoticeContent().replaceAll("/prod-api", UrlUtils.getUrl());
//        String url = sysNotice.getNoticeContent().replaceAll("//121.89.226.89:8083", UrlUtils.getUrl());
        //修改图片溢出问题
        String urlTo = url.replaceAll("<img", "<img style='max-width:100%;height:auto;'");
        sysNotice.setNoticeContent(urlTo);
        return AjaxResult.success(sysNotice);
    }

}
